package loader;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class ReflectDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {

        Class<Student> studentClass = Student.class;
        //System.out.println(studentClass);//class loader.Student

//        Student Student = new Student();
//        Class<? extends Student> aClass = Student.getClass();
//        System.out.println(studentClass==aClass);
//
//        Class<?> aClass1 = Class.forName("loader.Student");
//        System.out.println(aClass1==aClass);

        //Constructor<Student> constructor = studentClass.getConstructor();

       //Stream.of(constructor).forEach(System.out::println);

       // Stream.of(studentClass.getDeclaredConstructors()).forEach(System.out::println);


        Constructor<Student> constructor = studentClass.getConstructor();

        Student student = constructor.newInstance();
        System.out.println(student);

        System.out.println("---------");

        Class<?> aClass = Class.forName("loader.Student");
//        Constructor<?> constructor1 = aClass.getConstructor(String.class, int.class, String.class);
//
//        Object o = constructor1.newInstance("asd", 20, "dddiii");
//        System.out.println(o);
//
//        System.out.println("----------");


//        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
//        //暴力反射
//        declaredConstructor.setAccessible(true);
//        Object asdf = declaredConstructor.newInstance("asdf");
//        System.out.println( asdf);

        //Arrays.stream(aClass.getDeclaredFields()).toList().forEach(System.out::println);

        Field address = aClass.getField("address");
        //System.out.println(address);

        //获取无参构造方法获取对象 并创建实例
        Constructor<?> con = aClass.getConstructor();
        Object o = con.newInstance();

        address.set(o,"beijing");
        System.out.println(o);

        System.out.println("-------");


        Class<?> c = Class.forName("loader.Student");

        Constructor<?> cons = c.getConstructor();
        Object o1 = cons.newInstance();

        Field name = c.getDeclaredField("name");
        Field age = c.getDeclaredField("age");
        Field address1 = c.getDeclaredField("address");
        name.setAccessible(true);
        age.setAccessible(true);
        name.set(o1, "aaaaa");
        age.set(o1,39);
        address1.set(o1,"ddddd");

        System.out.println(o1);

        System.out.println("------------");
        //方法一得到
        Method method1 = c.getMethod("method1");
        //得到方法二
        Method method2 = c.getDeclaredMethod("method2", String.class);
        Method method3 = c.getDeclaredMethod("method3", String.class, int.class);

        method2.invoke(o1,"iiiii");
        Object iiiii = method3.invoke(o1, "iiiii", 12);
        method1.invoke(o1);
        System.out.println(iiiii);

        System.out.println(o1);


    }
}
